/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Daos;

import Clases.Personal;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Clases.Tarjeta;
import Clases.Usuario;
import Daos.exceptions.NonexistentEntityException;
import Daos.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author user
 */
public class PersonalJpaController implements Serializable {

    public PersonalJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Personal personal) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tarjeta idPing = personal.getIdPing();
            if (idPing != null) {
                idPing = em.getReference(idPing.getClass(), idPing.getPing());
                personal.setIdPing(idPing);
            }
            Usuario idLogin = personal.getIdLogin();
            if (idLogin != null) {
                idLogin = em.getReference(idLogin.getClass(), idLogin.getCodUser());
                personal.setIdLogin(idLogin);
            }
            em.persist(personal);
            if (idPing != null) {
                idPing.getPersonalList().add(personal);
                idPing = em.merge(idPing);
            }
            if (idLogin != null) {
                idLogin.getPersonalList().add(personal);
                idLogin = em.merge(idLogin);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPersonal(personal.getIdPer()) != null) {
                throw new PreexistingEntityException("Personal " + personal + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Personal personal) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Personal persistentPersonal = em.find(Personal.class, personal.getIdPer());
            Tarjeta idPingOld = persistentPersonal.getIdPing();
            Tarjeta idPingNew = personal.getIdPing();
            Usuario idLoginOld = persistentPersonal.getIdLogin();
            Usuario idLoginNew = personal.getIdLogin();
            if (idPingNew != null) {
                idPingNew = em.getReference(idPingNew.getClass(), idPingNew.getPing());
                personal.setIdPing(idPingNew);
            }
            if (idLoginNew != null) {
                idLoginNew = em.getReference(idLoginNew.getClass(), idLoginNew.getCodUser());
                personal.setIdLogin(idLoginNew);
            }
            personal = em.merge(personal);
            if (idPingOld != null && !idPingOld.equals(idPingNew)) {
                idPingOld.getPersonalList().remove(personal);
                idPingOld = em.merge(idPingOld);
            }
            if (idPingNew != null && !idPingNew.equals(idPingOld)) {
                idPingNew.getPersonalList().add(personal);
                idPingNew = em.merge(idPingNew);
            }
            if (idLoginOld != null && !idLoginOld.equals(idLoginNew)) {
                idLoginOld.getPersonalList().remove(personal);
                idLoginOld = em.merge(idLoginOld);
            }
            if (idLoginNew != null && !idLoginNew.equals(idLoginOld)) {
                idLoginNew.getPersonalList().add(personal);
                idLoginNew = em.merge(idLoginNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = personal.getIdPer();
                if (findPersonal(id) == null) {
                    throw new NonexistentEntityException("The personal with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Personal personal;
            try {
                personal = em.getReference(Personal.class, id);
                personal.getIdPer();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The personal with id " + id + " no longer exists.", enfe);
            }
            Tarjeta idPing = personal.getIdPing();
            if (idPing != null) {
                idPing.getPersonalList().remove(personal);
                idPing = em.merge(idPing);
            }
            Usuario idLogin = personal.getIdLogin();
            if (idLogin != null) {
                idLogin.getPersonalList().remove(personal);
                idLogin = em.merge(idLogin);
            }
            em.remove(personal);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Personal> findPersonalEntities() {
        return findPersonalEntities(true, -1, -1);
    }

    public List<Personal> findPersonalEntities(int maxResults, int firstResult) {
        return findPersonalEntities(false, maxResults, firstResult);
    }

    private List<Personal> findPersonalEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Personal.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Personal findPersonal(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Personal.class, id);
        } finally {
            em.close();
        }
    }

    public int getPersonalCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Personal> rt = cq.from(Personal.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
